// pages/cart/cart.js
Page({
  // 删除购物车内容
  onClose(e) {
    const { position } = e.detail;
    if (position === 'right') {
      const app = getApp();
      var index = e.currentTarget.dataset.index;
      app.globalData.cartList.splice(index, 1)
      this.setData({
        carts: app.globalData.cartList
      })
    }
  },
  // 调整全选状态
  adjustAllSelect() {
    var Allprice = 0, i = 0;
    //是否全选判断
    for (i = 0; i < this.data.carts.length; i++) {
      Allprice = Allprice + (this.data.carts[i].price * this.data.carts[i].count.quantity);
    }
    if (Allprice == this.data.totalMoney) {
      this.data.isAllSelect = true;
    }
    else {
      this.data.isAllSelect = false;
    }

    // 调整合计显示
    if (this.data.carts.length === 0) {
      this.data.totalMoney = 0;
    }
    this.setData({
      totalMoney: this.data.totalMoney,
      isAllSelect: this.data.isAllSelect
    })
  },
  //勾选事件处理函数  
  switchSelect: function (e) {
    // 获取item项的id，和数组的下标值  
    let index = parseInt(e.target.dataset.index);
    const { isSelect, price, count: { quantity } } = this.data.carts[index];
    this.data.carts[index].isSelect = !isSelect;
    //价钱统计
    if (this.data.carts[index].isSelect) {
      this.data.totalMoney = this.data.totalMoney + (price * quantity);
    }
    else {
      this.data.totalMoney = this.data.totalMoney - (price * quantity);
    }

    this.adjustAllSelect();

    this.setData({
      carts: this.data.carts,
      totalMoney: this.data.totalMoney
    })
  },
  //全选
  allSelect: function (e) {
    //处理全选逻辑
    let i = 0;
    if (!this.data.isAllSelect) {
      for (i = 0; i < this.data.carts.length; i++) {
        this.data.carts[i].isSelect = true;
        this.data.totalMoney = this.data.totalMoney + (this.data.carts[i].price * this.data.carts[i].count.quantity);
      }
    }
    else {
      for (i = 0; i < this.data.carts.length; i++) {
        this.data.carts[i].isSelect = false;
      }
      this.data.totalMoney = 0;
    }
    this.setData({
      carts: this.data.carts,
      isAllSelect: !this.data.isAllSelect,
      totalMoney: this.data.totalMoney,
    })
  },
  // 去结算
  toBuy() {
    if (this.data.totalMoney === 0) {
      wx.showToast({
        title: '请勾选商品',
        icon: 'error',
        duration: 2000
      });
      return false;
    }
    const app = getApp();
    this.data.carts.forEach((cart, index) => {
      if (cart.isSelect) {
        app.globalData.orders.push({
          ...cart,
          status: 1
        })
        this.data.carts.splice(index, 1)
      }
    })

    wx.showToast({
      title: '结算成功',
      icon: 'success',
      duration: 2000
    });

    this.adjustAllSelect();

    this.setData({
      carts: this.data.carts,
      showDialog: !this.data.showDialog
    });
  },
  //数量变化处理
  onChange(e) {
    var index = e.currentTarget.dataset.index;
    var quantity = e.detail;
    this.data.carts[index].count.quantity = quantity;
    if (this.data.carts[index].isSelect) {
      this.calcTotalMoney()
    }
    this.setData({
      carts: this.data.carts,
    });
  },
  calcTotalMoney() {
    let total = 0;
    this.data.carts.forEach(cart => {
      if (cart.isSelect) {
        total += cart.price * cart.count.quantity
      }
    })
    this.setData({
      totalMoney: total
    })
  },
  /**
   * 页面的初始数据
   */
  data: {
    isAllSelect: false,
    totalMoney: 0,
    carts: [{
      id: 2,
      title: "Redmi Note12 ",
      cover: "https://cdn.cnbj0.fds.api.mi-img.com/b2c-shopapi-pms/pms_1666797624.80788381.png",
      price: 1199,
      intro: "三星 OLED 护眼屏｜骁龙 5G 芯｜5000mAh 电量",
      isSelect: false,
      count: {
        quantity: 2,
        min: 1,
        max: 20
      },
    },
    {
      id: 3,
      title: "Redmi Note12 Pro ",
      cover: "https://cdn.cnbj0.fds.api.mi-img.com/b2c-shopapi-pms/pms_1666798004.73859024.png",
      price: 1699,
      intro: " IMX766 防抖相机｜OLED 柔性直屏｜67W 闪充",
      isSelect: false,
      count: {
        quantity: 2,
        min: 1,
        max: 20
      },
    }],
    goods: [{
      id: 2,
      title: "Redmi Note12 ",
      cover: "https://cdn.cnbj0.fds.api.mi-img.com/b2c-shopapi-pms/pms_1666797624.80788381.png",
      price: 1199,
      intro: "三星 OLED 护眼屏｜骁龙 5G 芯｜5000mAh 电量",
      isSelect: false,
      count: {
        quantity: 1,
        min: 1,
        max: 20
      },
    },
    {
      id: 3,
      title: "Redmi Note12 Pro ",
      cover: "https://cdn.cnbj0.fds.api.mi-img.com/b2c-shopapi-pms/pms_1666798004.73859024.png",
      price: 1699,
      intro: " IMX766 防抖相机｜OLED 柔性直屏｜67W 闪充",
      isSelect: false,
      count: {
        quantity: 1,
        min: 1,
        max: 20
      },
    },
    {
      id: 4,
      title: "Redmi Note12 Pro+",
      cover: "https://cdn.cnbj0.fds.api.mi-img.com/b2c-shopapi-pms/pms_1666797744.54085396.png",
      price: 2099,
      intro: "2亿超清防抖相机｜OLED 柔性直屏",
      isSelect: false,
    },
    {
      id: 5,
      title: "Redmi Note11",
      cover: "https://cdn.cnbj0.fds.api.mi-img.com/b2c-shopapi-pms/pms_1663832679.2515046.jpg",
      price: 1099,
      intro: "5000mAh大电量",
      isSelect: false,
      count: {
        quantity: 1,
        min: 1,
        max: 20
      },
    },
    {
      id: 6,
      title: "Redmi K50",
      cover: "https://cdn.cnbj0.fds.api.mi-img.com/b2c-shopapi-pms/pms_1647430877.23348470.png",
      price: 2299,
      intro: "2K直屏的狠旗舰",
      isSelect: false,
      count: {
        quantity: 1,
        min: 1,
        max: 20
      },
    },
    {
      id: 7,
      title: "Redmi Note11T Pro",
      cover: "https://cdn.cnbj0.fds.api.mi-img.com/b2c-shopapi-pms/pms_1667555442.44922504.jpg",
      price: 1569,
      intro: "天玑8100｜真旗舰芯",
      isSelect: false,
      count: {
        quantity: 1,
        min: 1,
        max: 20
      },
    },
    {
      id: 8,
      title: "Xiaomi 12S Pro",
      cover: "https://cdn.cnbj0.fds.api.mi-img.com/b2c-shopapi-pms/pms_1656916272.35595122.png",
      price: 4399,
      intro: "骁龙8+ 旗舰处理器 | 徕卡影像",
      isSelect: false,
      count: {
        quantity: 1,
        min: 1,
        max: 20
      },
    },
    {
      id: 9,
      title: "Redmi K50 至尊版",
      cover: "https://cdn.cnbj0.fds.api.mi-img.com/b2c-shopapi-pms/pms_1660117297.65076315.png",
      price: 2999,
      intro: "骁龙8+ ｜1.5K 高清直屏",
      isSelect: false,
      count: {
        quantity: 1,
        min: 1,
        max: 20
      },
    }
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.adjustAllSelect();

    const that = this
    const app = getApp()
    that.setData({
      carts: app.globalData.cartList
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})